<?php
    class PaymentRevenueController extends CpController
    {
        public function init()
        {
            $url = new Url();
            $this->breadcrumbs[] = array('name'=>'Đối soát thi chi','link'=>$url->createUrl("paymentrevenue/index"),'class'=>'');
        }
        public function actionIndex(){
            $data_confirm_year_value=0;
            $data_sms_year_value=0;
            $data_card_year_value=0;
            $data_charging_year_value=0;
            $total_card_price=0;
            $total_sms_price=0;
            $error="";
            $total=0;
            $total_tel=0;
            $total_ds=0;
            $total_confirm=0;
            $total_month=array();
            $total_telco=array();
            $total_dauso=array();
            $year=isset($_GET['year'])? intval($_GET['year']) : date('Y');
            $username=isset($_GET['username'])? $_GET['username'] : "";

            /*Lấy tỉ lệ sms*/
            $rate = APaymentRevenue::getAllRate();
            $rate_sms = APaymentRevenue::getAllRateSms();
            $rate_card = APaymentRevenue::getAllRateCard();
            if($username!=""){
                $login=APaymentRevenue::getUserId($username);
                if(empty($login)){
                    $error.="<p style='color: red'>- User bạn nhập không tồn tại</p>";
                    $username="";
                }
                $userId=$login['id'];
            } else{
                $userId='';
            }
            /*Lấy số dư wapmaster mỗi năm*/
            $data_confirm_year=APaymentRevenue::getDataConfirmYear($userId,$year);
            $data_sms_year=APaymentRevenue::getAllDataSmsYear($userId,$year-1);
            $data_card_year=APaymentRevenue::getAllDataCardYear($userId,$year-1);
            $data_charging_year=APaymentRevenue::getAllDataChargingYear($userId,$year-1);
            foreach($data_confirm_year as $key=>$value){
                $data_confirm_year_value=$data_confirm_year_value+$value['money'];
            }
            
            foreach($data_sms_year as $key=>$value){
                $index_rate = $value["month"] . "_" . $value["year"];
                $sc = preg_replace("/(\d+)(\d+)(\d+)(\d+)/si", "X$2XX", $value["serviceNumber"]);
                $type = $value["type"];
                $rateSms=$rate_sms[$index_rate][$sc]/100;
                if($value['type']==1){
                    $real_value=round($value['count']*$value['price']*0.85*$rateSms*($value['percent_wapmaster']/100));
                } 
                if($value['type']>1){
                    $real_value=round($value['count']*$value['price']*0.85*$rateSms*($rate[$index_rate][$type]['percent_wapmaster']/100));
                }
                $data_sms_year_value=$data_sms_year_value+$real_value;
            }
            foreach($data_card_year as $key=>$value){
                $rateCard=$rate_card[$value['month']][$value['year']]/100;
                $real_value=round($value['count']*$value['price'] * $rateCard *($value['percent_wapmaster']/100));
                $data_card_year_value=$data_card_year_value+$real_value;
            }
            foreach($data_charging_year as $key=>$value){
                
                $real_value=round($value['count']*$value['price_wapmaster']);
                $data_charging_year_value=$data_charging_year_value+$real_value;
            }
            $total_wap_year=$data_sms_year_value+$data_card_year_value+$data_charging_year_value;
            $wap_excess=array();
            if($year==2012){
                $wap_excess[$year][1]=0;
            }
            else{
                $wap_excess[$year][1]=$total_wap_year-$data_confirm_year_value;
            }
            
            /*Lấy doanh thu theo tháng */
            $data_sms=APaymentRevenue::getAllDataSms($userId,$year);
            $data_card=APaymentRevenue::getAllDataCard($userId,$year);
            $data_charging=APaymentRevenue::getAllDataCharging($userId,$year);
            $data_cofirm=APaymentRevenue::getDataConfirm($userId,$year);
            for($i=1;$i<13;$i++){
                $price_sms_month[$i]=0;
                $price_sms_telco[$i]=0;
                $price_sms_dauso[$i]=0;
                $price_card_telco[$i]=0;
                $price_card_dauso[$i]=0;
                $price_card_month[$i]=0;
                
                $price_charging_telco[$i]=0;
                $price_charging_dauso[$i]=0;
                $price_charging_month[$i]=0;
                $confirm_month[$i]=0;

                $total_sms_price_month[$i]=array();
                $total_card_price_month[$i]=array();
                foreach($data_sms as $key=>$value){
                    $index_rate = $value["month"] . "_" . $value["year"];
                    if($value['month']==$i){
                        $sc = preg_replace("/(\d+)(\d+)(\d+)(\d+)/si", "X$2XX", $value["serviceNumber"]);
                        $type = $value["type"];
                        $total_sms_price_month[$i][$key]=$value['price'];
                        $rateSms=$rate_sms[$index_rate][$sc]/100;
                        $real_telco=round($value['count']*$total_sms_price_month[$i][$key]*$rateSms);
                        $real_dauso=round($value['count']*$total_sms_price_month[$i][$key]*$rateSms*0.85);
                        if($value['type']==1){
                            $real_value=round($value['count']*$total_sms_price_month[$i][$key]*0.85*$rateSms*($value['percent_wapmaster']/100));
                        } 
                        if($value['type']>1){
                            $real_value=round($value['count']*$total_sms_price_month[$i][$key]*0.85*$rateSms*($rate[$index_rate][$type]['percent_wapmaster']/100));
                        }
                        $price_sms_telco[$i]=$price_sms_telco[$i]+$real_telco;
                        $price_sms_dauso[$i]=$price_sms_dauso[$i]+$real_dauso;
                        $price_sms_month[$i]=$price_sms_month[$i]+$real_value;
                    }
                }
                foreach($data_card as $key=>$value){
                    if($value['month']==$i){
                        $rateCard=$rate_card[$value['month']][$value['year']]/100;
                        $total_card_price_month[$i][$key]=$value['price'];
                        $real_value=round($value['count']*$total_card_price_month[$i][$key] * $rateCard *($value['percent_wapmaster']/100));
                        $real_telco=round($value['count']*$total_card_price_month[$i][$key]*$rateCard);
                        $real_dauso=round($value['count']*$total_card_price_month[$i][$key]*$rateCard);
                        $price_card_month[$i] = $price_card_month[$i] + $real_value;
                        $price_card_telco[$i]=$price_card_telco[$i]+$real_telco;
                        $price_card_dauso[$i]=$price_card_dauso[$i]+$real_dauso;
                    }
                }
                
                foreach($data_charging as $key=>$value){
                    if($value['month']==$i){
                        
                        $real_value=round($value['count']*$value['price_wapmaster']);
                        $real_telco=round($value['count']*$value['price_telco']);
                        $real_dauso=round($value['count']*$value['price_telco']);
                        $price_charging_month[$i] = $price_charging_month[$i] + $real_value;
                        $price_charging_telco[$i]=$price_charging_telco[$i]+$real_telco;
                        $price_charging_dauso[$i]=$price_charging_dauso[$i]+$real_dauso;
                    }
                }


                $total_month[$i]=$price_card_month[$i]+$price_sms_month[$i]+$price_charging_month[$i];
                $total_telco[$i]=$price_card_telco[$i]+$price_sms_telco[$i]+$price_charging_telco[$i];
                $total_dauso[$i]=$price_card_dauso[$i]+$price_sms_dauso[$i]+$price_charging_dauso[$i];
                $total=$total+$total_month[$i];
                $total_tel=$total_tel+$total_telco[$i];
                $total_ds=$total_ds+$total_dauso[$i];

            }
            /*Lấy xác nhận thanh toán theo tháng*/
            for($i=2;$i<=13;$i++){
                $confirm_month[$i-1]=0;
                foreach($data_cofirm as $key=>$value){
                    if($value['month']==$i){
                        $confirm_month[$i-1] = $confirm_month[$i-1] + $value['money'];
                    }
                }
                if($i==13){
                    $year=$year+1;
                    $month=1;
                    $data_cofirm=APaymentRevenue::getDataConfirmLastMonth($userId,$year,$month);
                    foreach($data_cofirm as $key=>$value){
                        $confirm_month[$i-1] = $confirm_month[$i-1] + $value['money'];
                    }
                }
                $total_confirm=$total_confirm+$confirm_month[$i-1];
            }
            $year=$year-1;
            $total_month_show=array();
            for($i=1;$i<13;$i++){
                $total_month_show[$i]=$total_month[$i];
                $total_month[$i]=$total_month[$i]+$wap_excess[$year][$i];
                $wap_excess[$year][$i+1]=$total_month[$i]-$confirm_month[$i];
            } 
            //var_dump($wap_excess) ;die;
            $this->render("index"
            ,array(
            "username"=>$username,
            "total_month"=>$total_month,
            "total_telco"=>$total_telco,
            "total_dauso"=>$total_dauso,
            "total_month_show"=>$total_month_show,
            "confirm_month"=>$confirm_month,
            "wap_excess"=>$wap_excess,
            "year"=>$year,
            "total"=>$total,
            "total_ds"=>$total_ds,
            "total_tel"=>$total_tel,
            "total_confirm"=>$total_confirm
            )
            );
        }
    }
?>
